<div ...attributes>
  {{#unless this.currentAdmin.permissions.admin_manage}}
    <div class="alert alert-primary"><a href="{{this.session.data.authenticated.admin_contact_url}}">Contact us</a> to make adminstrator changes.</div>
  {{/unless}}

  <ErrorMessages @model={{this.model}} />

  <form {{on "submit" this.submitForm}}>
    <FieldsFor @model={{this.model}} @style="horizontal" as |f|>
      <fieldset class="form-horizontal condensed">
        <legend>User Info</legend>

        {{#if this.currentAdmin.permissions.admin_manage}}
          {{f.text-field "username" label=this.usernameLabel}}
          {{#unless this.session.data.authenticated.username_is_email}}
            {{f.text-field "email" label=(t "Email")}}
          {{/unless}}
        {{else}}
          {{f.static-field "username" label=this.usernameLabel}}
          {{#unless this.session.data.authenticated.username_is_email}}
            {{f.static-field "email" label=(t "Email")}}
          {{/unless}}
        {{/if}}
        {{#if this.model.name}}
          {{f.static-field "name" label=(t "Name")}}
        {{/if}}
        {{#if this.currentAdmin.permissions.admin_manage}}
          {{f.textarea-field "notes" label=(t "Notes")}}
          {{#if this.model.id}}
            {{#unless this.model.currentSignInAt}}
              {{f.checkbox-field "sendInviteEmail" label="Resend invite email"}}
            {{/unless}}
          {{else}}
            {{f.checkbox-field "sendInviteEmail" label="Send invite email"}}
          {{/if}}
        {{/if}}
      </fieldset>

      {{#if this.model.authenticationToken}}
        {{#if this.session.data.authenticated.local_auth_enabled}}
          {{#if this.currentAdmin.permissions.admin_manage}}
            <fieldset class="form-horizontal condensed">
              <legend>{{t "Change Your Password"}}</legend>

              {{f.password-field "currentPassword" label=(t "Current Password")}}
              {{f.password-field "password" label=(t "New Password") hint=(sprintf (t "%d characters minimum") this.session.data.authenticated.password_length_min)}}
              {{f.password-field "passwordConfirmation" label=(t "Confirm New Password")}}
            </fieldset>
          {{/if}}
        {{/if}}

        <fieldset class="form-horizontal condensed">
          <legend>Admin API Access</legend>

          {{#f.static-field "authenticationToken" label=(t "Admin API Token")}}
            <span class="api-key">{{this.model.authenticationToken}}</span>
          {{/f.static-field}}
        </fieldset>
      {{/if}}

      {{#if (or this.currentAdmin.permissions.admin_manage this.currentAdmin.permissions.admin_view)}}
        <fieldset class="form-horizontal condensed" disabled={{this.isDisabled}}>
          <legend>Permissions</legend>

          {{f.checkboxes-field "groupIds" label=(t "Groups") options=this.groupOptions}}
          {{#if this.currentAdmin.superuser}}
            {{f.checkbox-field "superuser" label=(t "Superuser")}}
          {{/if}}
        </fieldset>
      {{/if}}

      <div class="row">
        <div class="col-sm-6">
          {{#if this.currentAdmin.permissions.admin_manage}}
            <button type="submit" class="btn btn-lg btn-primary save-button"><span class="btn-label">Save</span><span class="btn-loading-label"><FaIcon @icon="sync-alt" @spin={{true}} />Saving...</span></button>
          {{/if}}
        </div>
        <div class="col-sm-6 record-details">
          {{#if this.model.id}}
            Created: {{format-date this.model.createdAt}} by {{this.model.creator.username}}<br>
            Last Updated: {{format-date this.model.updatedAt}} by {{this.model.updater.username}}<br>
            {{#if this.model.currentSignInAt}}Last Login: {{format-date this.model.currentSignInAt}} from {{this.model.currentSignInIp}} via {{this.model.currentSignInProvider}}<br>{{/if}}
            Logged in: {{this.model.signInCount}} times<br>
          {{/if}}
        </div>
      </div>
      {{#if this.currentAdmin.permissions.admin_manage}}
        {{#if this.model.id}}
          <div class="form-extra-actions">
            <a href="#" class="remove-action" {{action "delete"}}><FaIcon @icon="times" />Delete Admin</a>
          </div>
        {{/if}}
      {{/if}}
    </FieldsFor>
  </form>
</div>
